package com.wxxymakers.cklabrecruit.controller;

import com.wxxymakers.cklabrecruit.entity.EmailRecord;
import com.wxxymakers.cklabrecruit.service.EmailTemplateService;
import com.wxxymakers.cklabrecruit.utils.RestResponse;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @AUTHOR soft
 * @DATE 2018/11/24 13:53
 * @DESCRIBE 邮件记录控制器
 */
@RestController
@RequiresRoles({"admin"})
@RequestMapping("/email/record")
public class EmailRecordController {

    private final EmailTemplateService service;

    @Autowired
    public EmailRecordController(EmailTemplateService service) {
        this.service = service;
    }

    @PostMapping("/page")
    public RestResponse page(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "7", required = false) Integer size) {
        Page<EmailRecord> page = service.page(pageNum, size, null);
        if (page != null) {
            return RestResponse.ok(page);
        }
        return RestResponse.fail("暂无邮件发送记录！");
    }

    /**
     * 清空所有记录
     */
    @PostMapping("/clear")
    public RestResponse clear() {
        if (service.clear()) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }
}
